﻿using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ConnectDatabase
{
    public partial class Form1 : Form
    {
        private MySqlDataAdapter _da;
        private DataSet _ds;
        public Form1()
        {
            InitializeComponent();
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myConString = "Host =localhost;Database=test;Username=root;Password=root";
            MySqlConnection conn = new MySqlConnection(myConString);
            try
            {
                string sql = "select * from for_test where pwd!='123456'";
                _da = new MySqlDataAdapter(sql, conn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(_da);
                _ds = new DataSet();
                _da.Fill(_ds, "for_test");
                dataGridView1.DataSource = _ds;
                dataGridView1.DataMember = "for_test";

            }
            catch (Exception ex)
            {
                MessageBox.Show(@"exception", ex.StackTrace);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _da.Update(_ds,"for_test");
            MessageBox.Show(@"更新成功", @"提示", MessageBoxButtons.OK);
        }
    }
}
